
package monitortest;

//import gnu.io.CommPortIdentifier;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author the Dope Factory
 */
public class Interface extends JFrame{
    
    /**Toolkit object - cointains kit tools*/
    Toolkit screen = Toolkit.getDefaultToolkit();
    
    /**Icon with gear symbol as favicon*/
    public static Image favicon;
    
    /**width of the default screen of the pc*/
    int xSize;
    
    /**height of the default screen of the pc*/
    int ySize;
    
    /** JmenuBar in the top of the frame*/
    JMenuBar menu;
    
    /**menu monitor*/
    JMenu monitor;
    
    /**menu test*/
    JMenu test;
    
    /**menu item console*/
    JMenuItem console;
    
    /**menu item per avvio monitoring via COM*/
    JMenuItem start;
    
    /**menu item per interruzione monitoring, precedentemente avviata via COM*/
    JMenuItem stop;
    
    /**menu item per avvio monitoring via server proxy*/
    JMenuItem proxyStart;
    
    /**menu item per interruzione monitoring, precedentemente avviata viA proxy*/
    JMenuItem proxyStop;
    
    /**Immagine di sfondo - planimetria*/
    ImageIcon backGround;
    JLabel backGroundL;
    JLabel prova=new JLabel(new ImageIcon("images/plan/background.png"),JLabel.CENTER);
    
    /*LED METER SWITCH#1*/ 
    /**Immagine bottone ROSSO*/
    public static JLabel glassyRedS1=new JLabel(new ImageIcon("images/icons/resize/red.png"),JLabel.CENTER);
    /**Immagine bottone BLUE*/
    public static JLabel glassyBlueS1=new JLabel(new ImageIcon("images/icons/resize/blue.png"),JLabel.CENTER);
     /**Immagine bottone GRIGIO*/
    public static JLabel glassyGreyS1=new JLabel(new ImageIcon("images/icons/resize/grey.png"),JLabel.CENTER);
    /**Immagine bottone GIALLO*/
    public static JLabel glassyYellowS1=new JLabel(new ImageIcon("images/icons/resize/yellow.png"),JLabel.CENTER);
    /**Immagine bottone VERDE*/
    public static JLabel glassyGreenS1=new JLabel(new ImageIcon("images/icons/resize/green.png"),JLabel.CENTER);    
    /**Immagine bottone ARANCIONE*/
    public static JLabel glassyOrangeS1=new JLabel(new ImageIcon("images/icons/resize/orange.png"),JLabel.CENTER);
    
    /*LED METER SWITCH#2*/ 
    /**Immagine bottone ROSSO*/
    public static JLabel glassyRedS2=new JLabel(new ImageIcon("images/icons/resize/red.png"),JLabel.CENTER);
    /**Immagine bottone BLUE*/
    public static JLabel glassyBlueS2=new JLabel(new ImageIcon("images/icons/resize/blue.png"),JLabel.CENTER);
     /**Immagine bottone GRIGIO*/
    public static JLabel glassyGreyS2=new JLabel(new ImageIcon("images/icons/resize/grey.png"),JLabel.CENTER);
    /**Immagine bottone GIALLO*/
    public static JLabel glassyYellowS2=new JLabel(new ImageIcon("images/icons/resize/yellow.png"),JLabel.CENTER);
    /**Immagine bottone VERDE*/
    public static JLabel glassyGreenS2=new JLabel(new ImageIcon("images/icons/resize/green.png"),JLabel.CENTER);    
    /**Immagine bottone ARANCIONE*/
    public static JLabel glassyOrangeS2=new JLabel(new ImageIcon("images/icons/resize/orange.png"),JLabel.CENTER);
    
    /*LED METER CONTROLLER */ 
    /**Immagine bottone ROSSO*/
    public static JLabel glassyRedC=new JLabel(new ImageIcon("images/icons/resize/red.png"),JLabel.CENTER);
    /**Immagine bottone BLUE*/
    public static JLabel glassyBlueC=new JLabel(new ImageIcon("images/icons/resize/blue.png"),JLabel.CENTER);
     /**Immagine bottone GRIGIO*/
    public static JLabel glassyGreyC=new JLabel(new ImageIcon("images/icons/resize/grey.png"),JLabel.CENTER);
    /**Immagine bottone GIALLO*/
    public static JLabel glassyYellowC=new JLabel(new ImageIcon("images/icons/resize/yellow.png"),JLabel.CENTER);
    /**Immagine bottone VERDE*/
    public static JLabel glassyGreenC=new JLabel(new ImageIcon("images/icons/resize/green.png"),JLabel.CENTER);    
    /**Immagine bottone ARANCIONE*/
    public static JLabel glassyOrangeC=new JLabel(new ImageIcon("images/icons/resize/orange.png"),JLabel.CENTER);
    
    /*LED METER DETECTOR*/ 
    /**Immagine bottone ROSSO*/
    public static JLabel glassyRedD=new JLabel(new ImageIcon("images/icons/resize/red.png"),JLabel.CENTER);
    /**Immagine bottone BLUE*/
    public static JLabel glassyBlueD=new JLabel(new ImageIcon("images/icons/resize/blue.png"),JLabel.CENTER);
     /**Immagine bottone GRIGIO*/
    public static JLabel glassyGreyD=new JLabel(new ImageIcon("images/icons/resize/grey.png"),JLabel.CENTER);
    /**Immagine bottone GIALLO*/
    public static JLabel glassyYellowD=new JLabel(new ImageIcon("images/icons/resize/yellow.png"),JLabel.CENTER);
    /**Immagine bottone VERDE*/
    public static JLabel glassyGreenD=new JLabel(new ImageIcon("images/icons/resize/green.png"),JLabel.CENTER);    
    /**Immagine bottone ARANCIONE*/
    public static JLabel glassyOrangeD=new JLabel(new ImageIcon("images/icons/resize/orange.png"),JLabel.CENTER);
    /**Color for background*/
    Color backFrame=new Color(102,102,102);
    
    /*DEFINE RECTANGLES FOR POSITION OF IMAGE STATUS*/
    /**Definition of pharameters of rectangles for SWITCH #1  @BathRoom*/
    Rectangle switch1=new Rectangle(560,180,35,35);
    
    /**Definition of pharameters of rectangles for SWITCH #2  @LivingRoom*/
    Rectangle switch2=new Rectangle(355,220,35,35);
    
    /**Definition of pharameters of rectangles for DETECTOR   @Garden / @OutsideRoom*/
    Rectangle detector=new Rectangle(965,150,35,35);
    
    /**Definition of pharameters of rectangles for CONTROLLER @LivingRoom*/
    Rectangle controller=new Rectangle(540,510,35,35);
    /*END OF DEFINITION RECTANGLES. */
    
    JLabel intro=new JLabel("Misurazioni domestiche:  ",JLabel.RIGHT);
    JLabel consumo=new JLabel("Assorbimento dell'abitazione:  ",JLabel.RIGHT);
    JLabel consS1=new JLabel("Assorbimento Switch 1:  ",JLabel.RIGHT);
    JLabel consS2=new JLabel("Assorbimento Switch 2:  ",JLabel.RIGHT);
    JLabel soglia=new JLabel("Soglia:  ",JLabel.RIGHT);
    JLabel perc=new JLabel("Percentuale superamento soglia:  ",JLabel.RIGHT);
    public static JTextField consTotT=new JTextField();
    public static JTextField consS1T=new JTextField();
    public static JTextField consS2T=new JTextField();
    public static JTextField sogliaT=new JTextField();
    public static JTextField percT=new JTextField();
    
    
    public Interface(){
        
        /**Try to set the Windows look and feel for swingc components*/
        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {}
        
        /*setting favicon*/
        favicon = screen.getImage("images/favicon/uman.png");
        
        /**Setting pharameters for the jframe: width & height*/
        xSize = ((int) screen.getScreenSize().getWidth());
        ySize = ((int) screen.getScreenSize().getHeight());  
        this.setSize(xSize,ySize-50);
        this.setLocation(0,0);
        this.setIconImage(favicon);
        this.setVisible(true);
        this.setTitle("Monitor");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        
        this.setLayout(null);
        
        /*setting menu*/
        setMenu();
        initComponent();
        /** Setting Image added in the JPanel in the background*/
        ImagePanel panel = new ImagePanel(new ImageIcon("images/plan/planimetria_v4.png").getImage());
        this.getContentPane().add(panel);
        panel.setBounds(-10, 0, xSize, ySize-50);
        
//        JLabel et1=new JLabel(new ImageIcon("images/icons/resize/blue.png"),JLabel.CENTER);
//        et1.setBounds(540,510,35,35);
//        panel.add(et1);
        
//         
//        JPanel pan=new JPanel();
//        this.add(pan);
//        Color alpha=new Color(0,0,0,0);
//        pan.setBackground(alpha);
//        pan.setLayout(null);
//        pan.setBounds(0,0,xSize,ySize-50);
//        pan.setOpaque(true);
//        JLabel et2=new JLabel(new ImageIcon("images/icons/square/blue.gif"));
//        pan.add(et2);
//        et2.setVisible(false);
//        glassyBlue=new ImageIcon("images/icons/blue.png");
////        glassyBlue.
//        et2.setOpaque(true);
//        et2.setBounds(200,200,30,30);
//        et2.setVisible(true);
//        JLabel et2=new JLabel(new ImageIcon("images/icons/blue.png"));
//        this.add(et2);
//        et2.setBounds(10,100,150,150);
//        backGround=new ImageIcon("images/plan/background.png");
//        glassyBlue=new ImageIcon("images/button/blue.png");
//        glassyRed=new ImageIcon("images/button/red.png");
//        glassyYellow=new ImageIcon("images/button/yellow.png");
//        glassyGreen=new ImageIcon("images/button/green.png");
//        glassyGrey=new ImageIcon("images/button/grey.png");
        
//        backGroundL=new JLabel(backGround);
////        this.add(backGroundL); 
//        this.add(prova);
////        backGroundL.setVisible(true);
//        JPanel pannello=new JPanel();
//        this.add(pannello);
//        pannello.add(prova,BorderLayout.CENTER);
        
        
        
        setSwitch1Visible(true, false, false, false, false, false);
        setSwitch2Visible(true, false, false, false, false, false);
        setControllerVisible(true, false, false, false, false, false);
        setDetectorVisible(true, false, false, false, false, false);
        
        this.setExtendedState(MAXIMIZED_BOTH);
    }
    
    
    public void setMenu(){
        menu=new JMenuBar();
        monitor=new JMenu("Monitor");
        test=new JMenu("Test");
        console=new JMenuItem("Console..");
        start=new JMenuItem("Serial Start");
        stop=new JMenuItem("Serial Stop");
        proxyStart=new JMenuItem("Proxy Start");
        proxyStop=new JMenuItem("Proxy Stop");
        this.setJMenuBar(menu);
        menu.add(monitor);
        menu.add(test);
        test.add(console);   
        monitor.add(start);        
        monitor.add(stop);
        monitor.addSeparator();
        monitor.add(proxyStart);
        monitor.add(proxyStop);
        
        stop.setEnabled(false);
        proxyStop.setEnabled(false);
        
        start.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                InterfacePort show=new InterfacePort();
            }
        });
    
        stop.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
//                SerialRead serial=new SerialRead();
//                serial.portClose();                    
                }
            });
        proxyStart.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                InterfaceProxy proxy=new InterfaceProxy();
            }
        });
    
        proxyStop.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                ProxyRead proxy=new ProxyRead();
                proxy.closeChannel();
            }
        });
    }
    
    /**
     * Initialize led status component.
     * 6 color of differents led, for 4 group of device.
     * The visibility of each component are setting off for default.
     */
    public void initComponent(){
        //adding switch#1 leds
        this.add(glassyRedS1);
        this.add(glassyBlueS1);
        this.add(glassyYellowS1);
        this.add(glassyOrangeS1);
        this.add(glassyGreyS1);
        this.add(glassyGreenS1);
        
        //adding switch#1 leds
        this.add(glassyRedS2);
        this.add(glassyBlueS2);
        this.add(glassyYellowS2);
        this.add(glassyOrangeS2);
        this.add(glassyGreyS2);
        this.add(glassyGreenS2);
        
        //adding scontroller leds
        this.add(glassyRedC);
        this.add(glassyBlueC);
        this.add(glassyYellowC);
        this.add(glassyOrangeC);
        this.add(glassyGreyC);
        this.add(glassyGreenC);
        
        //adding detector leds
        this.add(glassyRedD);
        this.add(glassyBlueD);
        this.add(glassyYellowD);
        this.add(glassyOrangeD);
        this.add(glassyGreyD);
        this.add(glassyGreenD);
        
        //pannello illustrativo
        this.add(intro);
        this.add(consumo);
        this.add(consS1);
        this.add(consS2);
        this.add(soglia);
        this.add(perc);
        this.add(consTotT);
        this.add(consS1T);
        this.add(consS2T);
        this.add(sogliaT);
        this.add(percT);
        
        intro.setBounds(1050,10,200,30);
        consumo.setBounds(1050,50,200,30);
        consTotT.setBounds(1250,50,80,25);
        consS1.setBounds(1050,80,200,30);
        consS1T.setBounds(1250,80,80,25);
        consS2.setBounds(1050,110,200,30);
        consS2T.setBounds(1250,110,80,25);
        soglia.setBounds(1050,140,200,30);
        sogliaT.setBounds(1250,140,80,25);
        perc.setBounds(1050,170,200,30);
        percT.setBounds(1250,170,80,25);
 
        //setting bounds for switch#1
        glassyRedS1.setBounds(switch1);
        glassyBlueS1.setBounds(switch1);
        glassyYellowS1.setBounds(switch1);
        glassyOrangeS1.setBounds(switch1);
        glassyGreyS1.setBounds(switch1);
        glassyGreenS1.setBounds(switch1);
        //initialize leds status off
        glassyRedS1.setVisible(false);
        glassyBlueS1.setVisible(false);
        glassyYellowS1.setVisible(false);
        glassyOrangeS1.setVisible(false);
        glassyGreyS1.setVisible(false);
        glassyGreenS1.setVisible(false);
        
        //setting bounds for switch#2
        glassyRedS2.setBounds(switch2);
        glassyBlueS2.setBounds(switch2);
        glassyYellowS2.setBounds(switch2);
        glassyOrangeS2.setBounds(switch2);
        glassyGreyS2.setBounds(switch2);
        glassyGreenS2.setBounds(switch2);
        //initialize leds status off
        glassyRedS2.setVisible(false);
        glassyBlueS2.setVisible(false);
        glassyYellowS2.setVisible(false);
        glassyOrangeS2.setVisible(false);
        glassyGreyS2.setVisible(false);
        glassyGreenS2.setVisible(false);
        
        //setting bounds for controller
        glassyRedC.setBounds(controller);
        glassyBlueC.setBounds(controller);
        glassyYellowC.setBounds(controller);
        glassyOrangeC.setBounds(controller);
        glassyGreyC.setBounds(controller);
        glassyGreenC.setBounds(controller);
        //initialize leds status off
        glassyRedC.setVisible(false);
        glassyBlueC.setVisible(false);
        glassyYellowC.setVisible(false);
        glassyOrangeC.setVisible(false);
        glassyGreyC.setVisible(false);
        glassyGreenC.setVisible(false);
        
        //setting bounds for detector
        glassyRedD.setBounds(detector);
        glassyBlueD.setBounds(detector);
        glassyYellowD.setBounds(detector);
        glassyOrangeD.setBounds(detector);
        glassyGreyD.setBounds(detector);
        glassyGreenD.setBounds(detector);        
        //initialize leds status off
        glassyRedD.setVisible(false);
        glassyBlueD.setVisible(false);
        glassyYellowD.setVisible(false);
        glassyOrangeD.setVisible(false);
        glassyGreyD.setVisible(false);
        glassyGreenD.setVisible(false);
        
        
    }
    
    /**
     * Setting the visibility of each component for group of Switch #1
     * @param visRed
     * @param visBlue
     * @param visYellow
     * @param visOrange
     * @param visGrey
     * @param visGreen 
     */
    public static void setSwitch1Visible(boolean visRed, boolean visBlue, 
                                  boolean visYellow, boolean visOrange, 
                                  boolean visGrey, boolean visGreen ){
        glassyRedS1.setVisible(visRed);
        glassyBlueS1.setVisible(visBlue);
        glassyYellowS1.setVisible(visYellow);
        glassyOrangeS1.setVisible(visOrange);
        glassyGreyS1.setVisible(visGrey);
        glassyGreenS1.setVisible(visGreen);
    }
    
    /**
     * Setting the visibility of each component for group of Switch #2
     * @param visRed
     * @param visBlue
     * @param visYellow
     * @param visOrange
     * @param visGrey
     * @param visGreen 
     */
    public static void setSwitch2Visible(boolean visRed, boolean visBlue, 
                                  boolean visYellow, boolean visOrange, 
                                  boolean visGrey, boolean visGreen ){
        glassyRedS2.setVisible(visRed);
        glassyBlueS2.setVisible(visBlue);
        glassyYellowS2.setVisible(visYellow);
        glassyOrangeS2.setVisible(visOrange);
        glassyGreyS2.setVisible(visGrey);
        glassyGreenS2.setVisible(visGreen);
    }
    
    /**
     * Setting the visibility of each component for group of Controller
     * @param visRed
     * @param visBlue
     * @param visYellow
     * @param visOrange
     * @param visGrey
     * @param visGreen 
     */
    public static void setControllerVisible(boolean visRed, boolean visBlue, 
                                  boolean visYellow, boolean visOrange, 
                                  boolean visGrey, boolean visGreen ){
        glassyRedC.setVisible(visRed);
        glassyBlueC.setVisible(visBlue);
        glassyYellowC.setVisible(visYellow);
        glassyOrangeC.setVisible(visOrange);
        glassyGreyC.setVisible(visGrey);
        glassyGreenC.setVisible(visGreen);
    }
    
    /**
     * Setting the visibility of each component for group of Detector
     * @param visRed
     * @param visBlue
     * @param visYellow
     * @param visOrange
     * @param visGrey
     * @param visGreen 
     */
    public static void setDetectorVisible(boolean visRed, boolean visBlue, 
                                  boolean visYellow, boolean visOrange, 
                                  boolean visGrey, boolean visGreen ){
        glassyRedD.setVisible(visRed);
        glassyBlueD.setVisible(visBlue);
        glassyYellowD.setVisible(visYellow);
        glassyOrangeD.setVisible(visOrange);
        glassyGreyD.setVisible(visGrey);
        glassyGreenD.setVisible(visGreen);
    }   
}